package com.xhj.sportsnews.fragment;

import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.support.v4.util.Pair;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;

import com.xhj.sportsnews.BR;
import com.xhj.sportsnews.R;
import com.xhj.sportsnews.adapter.RecommendRecycleAdapter;
import com.xhj.sportsnews.bean.Gaming;
import com.xhj.sportsnews.callback.HttpService;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.Map;

import retrofit2.Call;
import retrofit2.Callback;
import retrofit2.Response;
import retrofit2.Retrofit;
import retrofit2.converter.gson.GsonConverterFactory;

/**
 * Created by Administrator on 2016/12/20.
 */

public class News_DepthFragment extends Fragment implements Callback<Gaming> {
    private RecyclerView recycleView;
    private RecommendRecycleAdapter<Gaming.Data.Contents> adapter;
    private HttpService service;

    public News_DepthFragment() {
        // Required empty public constructor
    }


    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
        View rootView=inflater.inflate(R.layout.fragment_news__recommend, container, false);
        initView(rootView);
        return rootView;
    }

    private void initView(View rootView) {
        recycleView= (RecyclerView) rootView.findViewById(R.id.recommend_recycle);
        Map<Class<?>,Pair<Integer,Integer>> map=new HashMap<>();
        map.put(null,Pair.create(R.layout.news_gaming, BR.game));
        adapter= new RecommendRecycleAdapter<Gaming.Data.Contents>(getContext(),new ArrayList<Gaming.Data.Contents>(),map);
        recycleView.setAdapter(adapter);
        service= new Retrofit.Builder()
                .baseUrl("http://api.lifxapi.com/tiyu/rest/content/")
                .addConverterFactory(GsonConverterFactory.create())
                .build()
                .create(HttpService.class);
        service.getDepth().enqueue(this);
    }

    @Override
    public void onResponse(Call<Gaming> call, Response<Gaming> response) {
        Gaming body = response.body();
        adapter.addAll(body.getData().getContents());
    }

    @Override
    public void onFailure(Call<Gaming> call, Throwable t) {

    }

}
